package software.literature.model;

import java.lang.reflect.Field;

import software.literature.config.ConfigureData;

public class BBG {
	private int id = -1;
	private String data = "";
	private String url = "";
	private int treeId = -1;
	private int type;
	private String noteText = "";
	private String noteUrl = "";
	static {
		// this is according to NewBBG.types
	}


	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		if (url != null) {
			this.url = url;
		} else {
			url = "";
		}
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}


	public String getNoteText() {
		return noteText;
	}

	public void setNoteText(String noteText) {
		if (noteText != null) {
			this.noteText = noteText;
		} else {
			noteText = "";
		}
	}

	public String getNoteUrl() {
		return noteUrl;
	}

	public void setNoteUrl(String noteUrl) {
		if (noteUrl != null) {
			this.noteUrl = noteUrl;
		} else {
			noteUrl = "";
		}
	}

	public String[] getTableFormat() {
		// TODO Auto-generated method stub
		String className = ConfigureData.getInstance().dataMap.get(type);
		try {
			Class dataClass = Class.forName(className);
			BBGData data = (BBGData) dataClass.newInstance();
			String[] s = this.data.split("##");
			Field[] f = dataClass.getDeclaredFields();
			int index = 0;
			for (int i = 0; i < s.length; i++) {
				// when insert ,if the field is null or is "  ",then it will be
				// stored as " "
				if (s[i].equals(" ")) {
					f[i].set(data, "");
				} else {
					f[i].set(data, s[i]);
				}
			}
			return data.getTableFormat();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public BBGData getBBGData(int i) {
		// TODO Auto-generated method stub
		switch (i) {
		case 0: {
            BBGData data=new Monograph();
            data.parse(this);
		}
		case 1: {
			 BBGData data=new Journal();
	            data.parse(this);
		}
		case 2: {
			 BBGData data=new Proceedings();
	            data.parse(this);
		}
		case 3: {
			 BBGData data=new TechnicalReport();
	            data.parse(this);
		}
		case 4: {
			 BBGData data=new ThesesOrDissertation();
	            data.parse(this);
		}
		case 5: {
			 BBGData data=new URLType();
	         data.parse(this);
		}
		}
		return null;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getTreeId() {
		return treeId;
	}

	public void setTreeId(int treeId) {
		this.treeId = treeId;
	}
}
